//
//  PQStatement.m
//  ObjectivePQ
//
//  Created by Ian Michell on 08/07/2007.
//  Copyright 2007 Ian Michell. All rights reserved.
//

#import "PQStatement.h"


// Used as a c++ friend style hack ;)
@interface PQConnection (PQStatement)
- (PGconn*) getPGConnection;
@end

@implementation PQConnection (PQStatement)
- (PGconn*) getPGConnection
{
	return self->connection;
}
@end

// This will allow us to access a native result set object
@interface PQResultSet (PQStatement)
- (id <DBResultSet>) initWithNativeResult: (PGresult*) r;
@end

@implementation PQResultSet (PQStatement)
- (id <DBResultSet>) initWithNativeResult: (PGresult*) r
{
	self = [super init];
	if (self)
	{
		self->res = r;
		self->currentRow = -1;
	}
	return self;
}
@end

@implementation PQStatement

//==============================================================================
// Query Methods
//==============================================================================
- (bool) execute
{
	bool retVal = FALSE;
	PGresult* res = PQexec([self->conn getPGConnection], [[self getQuery] UTF8String]);
	self->errorMessage = [[NSString alloc] initWithCString: PQcmdTuples(res)];
	switch (PQresultStatus(res))
	{
		// Commands like this should use affected rows
		case PGRES_COMMAND_OK:
			retVal = TRUE;
			self->affectedRows = 
			PQclear(res);
			break;
		// We have a result set!
		case PGRES_TUPLES_OK:
			self->result = (PQResultSet*)[[PQResultSet alloc] initWithNativeResult: res];
			retVal = TRUE;
			break;
		// Maybe set a message?
		case PGRES_COPY_OUT:
			retVal = TRUE;
			PQclear(res);
			break;
		case PGRES_COPY_IN:
			retVal = TRUE;
			PQclear(res);
			break;
		// Oops we have an error!
		case PGRES_BAD_RESPONSE:
			retVal = FALSE; // Just set it anyway! :)
			PQclear(res);
			break;
		// WE have a warning message
		case PGRES_NONFATAL_ERROR:
			retVal = TRUE; // We set to true, but set a message too
				       // FIXME: set message here
			self->errorMessage = [[NSString alloc] initWithCString: PQresultErrorMessage(res)];
			PQclear(res);
			break;
		case PGRES_FATAL_ERROR:
			retVal = FALSE;
			self->errorMessage = [[NSString alloc] initWithCString: PQresultErrorMessage(res)];
			PQclear(res);
			@throw [NSException exceptionWithName:@"SQLException" reason: self->errorMessage
				userInfo: nil];
			break;
	}
	return retVal;	
}


//==============================================================================
// Result Methods
//==============================================================================
- (id <DBResultSet>) getResultSet
{
	return self->result;
}

- (int) getAffectedRows
{
	return self->affectedRows;
}

//==============================================================================
// Error Methods
//==============================================================================

- (NSString*) getErrorMessage
{
	return self->errorMessage;
}

//==============================================================================
// Getter Methods
//==============================================================================
- (NSString*) getQuery
{
	return self->query;
}

- (void) setQuery: (NSString*) sql
{
	self->query = sql;
}


- (void) close
{
	
}
@end
